<?php


namespace app\index\controller;
use think\Controller;

class MeritPayGrant extends Controller{

    public function index(){
        return $this->fetch();
    }

    public function getMeritPayGrant(){
        $page = request()->param('page');
        $limit = request()->param('limit');
        $start = ($page-1) * $limit;

        $infoList = array();
        $personInfoList = Db('base_info')
            ->join('user','base_info.user_id = user.id')
            ->join('company','base_info.company_id = company.id')
            ->select();
        foreach ($personInfoList as $personInfo){
            $meritPayCut = Db('merit_pay_cut')
                ->where([
                    'user_id'=> $personInfo['user_id'],
                    'isdelete' => 0
                ])
                ->find();
            if($meritPayCut != null){
                $info = array(
                    'id'=> $meritPayCut['id'],
                    'salary_job_type' => $personInfo['salary_job_type'],
                    'name' => $personInfo['name'],
                    'wage_date' => $meritPayCut['wage_date'],
                    'company' => $personInfo['company'],
                    'teaching_task_fee' => $meritPayCut['teaching_task_fee'],
                    'manage_fee' => $meritPayCut['manage_fee'],
                    'vacancy_subsidy' => $meritPayCut['vacancy_subsidy'],
                    'special_subsidy' => $meritPayCut['special_subsidy'],
                    'annual_assess_fee' => $meritPayCut['annual_assess_fee'],
                    'principal_award_fund' => $meritPayCut['principal_award_fund'],
                    'deduction_major_liability' => $meritPayCut['deduction_major_liability'],
                    'isgrant' => $meritPayCut['isgrant'],
                    'mediation_funds' => $meritPayCut['mediation_funds']
                );
                array_push($infoList,$info);
            }

        }

        $count =  Db('user')
            ->limit($start,$limit)
            ->count();

        $msg = [
            'code' => 0,
            'msg' => '查询成功',
            'data' => $infoList,
            'count' => $count
        ];

        echo json_encode($msg);

    }

    public function grantSelect(){
        //接收参数
        $map = input();
        
        $IDs=implode(',', $map["id"]);
        
        $where = 'id in('.$IDs.')';

        $infoList = Db('merit_pay_cut')->where($where)->select();
        foreach($infoList as $info){
            if($info['isgrant'] == 1){
                $this->error( '发放失败,请检查选中的绩效工资是否发放');
            }
        }


        $list = Db('merit_pay_cut')
            ->where($where)
            ->update([
                'isgrant' =>"1"
            ]);
        if($list!==false) {
            $this->success('发放成功');
        }else{
            $this->error( '发放失败');
        }

    }

    public function grant(){
        if(request()->param('id')){
            $id     = request()->param('id'); //获取id
            
            $status = Db('merit_pay_cut')
                ->where('id',$id)
                ->update([
                    'isgrant' =>"1"
                ]);
            if($status){
                $this->success('发放成功');
            }else{
                $this->error( '发放失败');
            }
        }
    }

}